﻿using Mforj.Common.Utils;
using smforj.common.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace smforj.common.service
{
    public interface ILoginService
    {
        /// <summary>
        /// 登陆信息
        /// </summary>
        /// <param name="username">用户名称</param>
        /// <param name="password">密码</param>
        /// <returns>返回登陆后信息</returns>
        LoginResponse login(String username,String password);

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="token">登陆的token</param>
        /// <returns>返回注销后信息</returns>
        LoginResponse logout(String token);
    }

    /// <summary>
    /// 登陆Service 实现
    /// </summary>
    public class LoginServiceImpl : ILoginService
    {


        private HttpHelper httpHelper = new HttpHelper();
        private JsonHelper jsonHelper = new JsonHelper();


        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public LoginResponse login(string username, string password)
        {
            String jsonData = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}";

            String url = GlobalCache.LoginUrl;
            String response = httpHelper.PostRequestJson(url, jsonData);

            LoginResponse cres = jsonHelper.JsonDeSerializer<LoginResponse>(response);
            if (cres == null)
                cres = new LoginResponse() { isSuccess = false, message = response };

            return cres;
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public LoginResponse logout(string token)
        {
            String url = GlobalCache.LogoutUrl + "?token=" + token;
            String response = httpHelper.GetRequestJson(url);

            LoginResponse cres = jsonHelper.JsonDeSerializer<LoginResponse>(response);
            if (cres == null)
                cres = new LoginResponse() { isSuccess = false, message = response };

            return cres;
        }
    }
}
